import * as api from '../services/api'

export default {
  namespace: 'product',
  state: {
    productList: [
      {
        id: 1,
        name: '豆子'
      },
      {
        id: 2,
        name: '玉米'
      },
      {
        id: 3,
        name: '土豆'
      }
    ]
  },
  reducers: {
    updateList(state, action) {
      console.log('action', action)
      let currentProductList = deepClone(state)
      currentProductList.productList.push(action.payload)
      return currentProductList
    }
  },
  effects: {
    *updateListAsync({ payload }, { call, put }) {
      yield put({
        type: 'updateList',
        payload
      })
    },
    *updateListHttp({ payload }, { call, put }) {
      //网络请求 payload是请求参数
      console.log('payload', payload)
      const result = yield call(api.login, payload)
      console.log('result', result)
      const data = result.data
      console.log('data',data)
      
      // if (data) {
      //   yield put({
      //     type: 'updateList',
      //     payload: data
      //   })
      // }
    }
  }
}

function deepClone(arr) {
  let _obj = JSON.stringify(arr),
    objClone = JSON.parse(_obj)
  return objClone
}
